package com.shaowc.gs.spring.beans;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.*;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ResourceLoader;

/**
 * created by shaowenchao
 */
public class Lifecycle implements BeanNameAware, BeanClassLoaderAware, BeanFactoryAware, ResourceLoaderAware, ApplicationEventPublisherAware,
        MessageSourceAware, ApplicationContextAware, InitializingBean, DisposableBean {

    int step;

    private String name;

    public void setBeanName(String name) {
        System.out.println((++ step) + ". BeanNameAware.setBeanName");
        this.name = name;
    }

    public void setBeanClassLoader(ClassLoader classLoader) {
        System.out.println((++ step) + ". BeanClassLoaderAware.setBeanClassLoader");
        System.out.println("ClassLoader: " + classLoader.getClass().getName());
    }

    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println((++ step) + ". BeanFactoryAware.setBeanFactory");
    }

    public void setResourceLoader(ResourceLoader resourceLoader) {
        System.out.println((++ step) + ". ResourceLoaderAware.setResourceLoader");
    }

    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        System.out.println((++ step) + ". ApplicationEventPublisherAware.setApplicationEventPublisher");
    }

    public void setMessageSource(MessageSource messageSource) {
        System.out.println((++ step) + ". MessageSourceAware.setMessageSource");
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println((++ step) + ". ApplicationContextAware.setApplicationContext");
    }

    public void destroy() throws Exception {
        System.out.println((++ step) + ". DisposableBean.destroy");
    }

    public void afterPropertiesSet() throws Exception {
        System.out.println((++ step) + ". InitializingBean.afterPropertiesSet");
    }

    public void init() {
        System.out.println((++ step) + ". custom init method");
    }

    public void customDestroy() {
        System.out.println((++ step) + ". custom destroy method");
    }

    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        applicationContext.start();
        applicationContext.close();
    }

}
